package com.tempo.widget;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;

/**
 * Created by tempo on 2015/11/16.
 */
public class SectionBarBottom extends FrameLayout implements View.OnClickListener {
    private ImageView ivRefresh;
    private View btnRefresh;
    private boolean isRefreshing;
    private OnRefreshListener mOnRefreshListener;

    public SectionBarBottom(Context context) {
        this(context,null);
    }

    public SectionBarBottom(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SectionBarBottom(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.sectionbar_bottom_layout, this, true);
        View divider=new View(context);
        LayoutParams params=new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) getResources().getDimension(R.dimen.sectionbar_bottom_divider_height));
        params.gravity= Gravity.BOTTOM;
        divider.setLayoutParams(params);
        divider.setBackgroundColor(Color.parseColor("#EEEEEE"));
        addView(divider);
        init();
    }

    private void init() {

        ivRefresh = (ImageView) findViewById(R.id.iv_refresh);
        btnRefresh = findViewById(R.id.btn_refresh);
        btnRefresh.setOnClickListener(this);
        ProgressBar p = new ProgressBar(getContext());

    }
    public void setOnRereshListener(OnRefreshListener onClickListener){
        mOnRefreshListener=onClickListener;
    }
    public interface OnRefreshListener{
        public void onRefresh();
    }
    @Override
    public void onClick(View v) {
        if(mOnRefreshListener!=null){
            if(!isRefreshing){
                isRefreshing=true;
                startRefesh();
                mOnRefreshListener.onRefresh();
            }
        }
    }

    private void startRefesh() {
        ivRefresh.clearAnimation();
        Animation rotate=AnimationUtils.loadAnimation(getContext(),R.anim.infinate_rotate);
//        RotateAnimation rotate=new RotateAnimation(0,-359, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
//        rotate.setFillAfter(true);
//        rotate.setDuration(700);
//        rotate.setRepeatMode(Animation.INFINITE);
        ivRefresh.startAnimation(rotate);

    }
    public void stopRefresh(){
        isRefreshing=false;
        ivRefresh.clearAnimation();

    }
}
